<?php
namespace catchAdmin\storehouse\excl;

use catchAdmin\storehouse\model\InventoryOperationRecord as InventoryOperationRecordModel;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class inventoryOperationRecordExport implements ExcelContract
{
    protected $model;
    
    public function __construct(InventoryOperationRecordModel $model)
    {
        // 设置内存限制
        ini_set('memory_limit', '1024M');
        // 设置执行时间
        set_time_limit(600);  // 10分钟
        
        $this->model = $model;
    }

    /**
     * 设置头部
     *
     * @return array
     */
    public function headers(): array
    {
        return [
            'ID',
            '订单ID', 
            '子订单ID',
            '订单类型',
            '商品料号',
            '包装规格',
            '托盘数量',
            '箱数',
            '数量',
            '库存入库订单号',
            '库存编号',
            '托盘码',
            '箱号',
            '片号',
            '客户业务单号',
            'LOT1',
            'LOT2', 
            'LOT3',
            '操作人',
            '创建时间'
        ];
    }

    /**
     * 处理数据
     *
     * @return array
     */
    public function sheets()
    {
        // 设置每批处理的数量
        $batchSize = 1000;
        $page = 1;
        $allData = [];
        
        while(true) {
            $list = $this->model->getExportList($page, $batchSize);
            
            if (empty($list)) {
                break;
            }
            
            foreach ($list as &$item) {
                // 处理订单类型
                if($item['order_type'] == 1) {
                    $item['order_type'] = '入库';
                } else if($item['order_type'] == 2) {
                    $item['order_type'] = '拆箱';  
                } else if($item['order_type'] == 3) {
                    $item['order_type'] = '拆件';
                } else if($item['order_type'] == 4) {
                    $item['order_type'] = '移库';
                } else if($item['order_type'] == 5) {
                    $item['order_type'] = '出库';
                }
            }
            
            $allData = array_merge($allData, $list);
            $page++;
            
            // 释放内存
            unset($list);
            gc_collect_cycles();
        }
        
        return $allData;
    }

    /**
     * 设置开始行
     *
     * @return int
     */
    public function setRow()
    {
        return 2;
    }

    /**
     * 设置列宽
     *
     * @return array
     */
    public function setWidth()
    {
        return [
            'A' => 10,
            'B' => 20,
            'C' => 20,
            'D' => 15,
            'E' => 20,
            'F' => 30,
            'G' => 15,
            'H' => 10,
            'I' => 10,
            'J' => 30,
            'K' => 30,
            'L' => 30,
            'M' => 30,
            'N' => 30,
            'O' => 30,
            'P' => 20,
            'Q' => 20,
            'R' => 20,
            'S' => 20,
            'T' => 20
        ];
    }

    /**
     * 设置标题
     *
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:T1', '库存操作记录', Alignment::HORIZONTAL_CENTER
        ];
    }
} 